package net.zjitc.link;

import net.domain.Student;
import net.zjitc.list.MyList;

public class MyLink<T> implements MyList<T> {
    private int size;
    private Node<T> first;
    private Node<T> last;
    @Override
    public boolean add(T date) {
        Node<T> node=new Node<>(date);
        linklast((Node<T>) node);
        return true;
    }

    private void linklast(Node<T> node) {
        if (first == null) {
                first = node;
                last = first;
                size++;
            } else {
                last.next = node;
                last = last.next;
                size++;

        }
    }

    @Override
    public boolean add(int index, T date) {
        if (index < 0||index > size){
            throw new RuntimeException("插入下标越界");
        }
        Node<T> node=new Node<T>(date);
         if (index==size){// size==0 判断是否为空
             //last
             linklast(node);
             return  true;
         }
             if (index==0){
             //first
             node.next=first;
             first=node;
             size++;
             return true;
        }
             //如何找到前一个
           Node<T> p=first;
             for (int i = 0; i < index-1; i++) {
                 p=p.next;
             }
             //插入
             node.next=p.next;
             p.next=node;
             size++;

        return true;
    }

    @Override
    public boolean set(int index, T date) {
        Node<T> p = getNode(index);
        p.date=date;
        return true;
    }

    @Override
    public T remove(T date) {
       Node<T> p=first;
       if (date==null){
           while (p!=null){
               if (p.date==null){

               }
               p=p.next;
           }
       }else {
           while (p!=null){
               if (date.equals(p.date)){

               }
           }
       }

        return null;
    }

    @Override
    public T remove(int index) {
        if (index < 0||index >= size){
            throw new RuntimeException("插入下标越界");
        }
        Node<T> p=first;
        T oldDate=null;
        if (index==0){
            first=p.next;
            p.next=null;
            size--;
            oldDate=p.date;
        }else{  //删除最后一个和中间处理方式是一样的，不一样的地方在于删除最后一个时候需要修改last
            for (int i = 0; i < index-1; i++) {
                p=p.next;
            }
            oldDate=p.next.date;
            p.next=p.next.next;
            if (index==size-1){
                last=p;

            }
            size--;
        }
        return oldDate;
    }

    @Override
    public T getValue(int index) {
        Node<T> p = getNode(index);

        return p.date;
    }

    private Node<T> getNode(int index) {
        if (index < 0 || index >= size) {
            throw new RuntimeException("下标越界");
        }
        Node<T> p = first;
        for (int i = 1; i < index; i++) {
            p = p.next;
        }
        return p;
    }

    @Override
    public int size() {
        return size;
    }

}
